from django.apps import AppConfig

class SensorsConfig(AppConfig):
    name = 'sensors'

    def ready(self):
        from .models import SystemSitting
        from .models import DevInfo
        from sensors.bottomfac.config import Config
        from sensors.bottomfac.bottom import tcp
        #
        # ------系统初始化-------
        print('-----Sys Init-----')
        Config.sysInitState = False
        #startScheduler()                #TODO 定时器
        # 初始化数据库
        try:
            # 初始化校准时间状态为0
            targetDev = SystemSitting.objects.get(type_name='time_calibrated')
            targetDev.status = 0
            targetDev.save()
            # 初始化继电器状态为close
            targetDev1 = SystemSitting.objects.get(type_name='manual')
            targetDev1.status = 1
            targetDev1.save()
            # 初始化继电器状态为close
            devList = DevInfo.objects.all()
            for i in devList:
                i.status = 'close'
                i.save()

            print('-----Sys Init data base-----')
        except:
            print('-----Sys Init data base err-----')

        # # 初始化tcpSever
        # try:
        #     #tcp.tcpInit()
        #     print('-----Sys Init tcp server-----')
        # except:
        #     print('-----Sys Init tcp server err-----')
        #初始化完成
        Config.sysInitState = True
